package com.quxiu.system.controller;

import com.quxiu.common.core.exception.ServiceException;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.web.page.TableDataInfo;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.common.security.utils.SecurityUtils;
import com.quxiu.system.domain.BpmProcInst;
import com.quxiu.system.domain.vo.BpmApprovalVo;
import com.quxiu.system.domain.vo.BpmApproveSearchParam;
import com.quxiu.system.domain.vo.BpmRollbackNodeVo;
import com.quxiu.system.service.IBpmApprovalService;
import com.quxiu.system.service.IBpmProcInstService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作流审批Controller
 *
 * @author quxiu
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/bpm/approval")
public class BpmApprovalController extends BaseController {
    @Resource
    private IBpmApprovalService bpmApprovalService;
    @Resource
    private IBpmProcInstService bpmProcInstService;

    /**
     * 审批
     */
    @PostMapping("/approval")
    public AjaxResult doApproval(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.approval(params);
        return success();
    }

    /**
     * 撤消
     */
    @PostMapping("/revoke")
    public AjaxResult revoke(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.revoke(params);
        return success();
    }

    /**
     * 加签
     */
    @PostMapping("/addSign")
    public AjaxResult addSign(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.addSign(params);
        return success();
    }

    /**
     * 转交
     */
    @PostMapping("/forward")
    public AjaxResult forward(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.forward(params, false);
        return success();
    }


    /**
     * 查询流程交接列表
     */
    @RequiresPermissions("bpm:handover")
    @GetMapping("/handoverList")
    public TableDataInfo handoverList(BpmApproveSearchParam param) {
        if (param.getUserId() == null) {
            throw new ServiceException("请选择当前审批人");
        }
        List<BpmProcInst> list = bpmProcInstService.handoverList(param);
        return getDataTable(list);
    }

    /**
     * 流程交接
     */
    @RequiresPermissions("bpm:handover")
    @PostMapping("/handover")
    public AjaxResult forwardByAdmin(@RequestBody BpmApprovalVo params) {
        if (params.getToUserId() == null) {
            return error("转交用户不能为空");
        }
        if (CollectionUtils.isEmpty(params.getRecordIds())) {
            return error("记录不能为空");
        }
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.forwardByAdmin(params);
        return success();
    }

    /**
     * 回退
     */
    @PostMapping("/rollback")
    public AjaxResult rollback(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        bpmApprovalService.rollback(params);
        return success();
    }

    /**
     * 获取回退节点列表
     */
    @PostMapping("/getRollbackNodes")
    public AjaxResult getRollbackNodes(@RequestBody BpmApprovalVo params) {
        Long userId = SecurityUtils.getUserId();
        params.setHandlerId(userId);
        List<BpmRollbackNodeVo> rollbackNodes = bpmApprovalService.getRollbackNodes(params);
        return success(rollbackNodes);
    }
}
